package com.example.constellation.starfrag;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.viewpager.widget.ViewPager;
import com.example.constellation.R;
import com.example.constellation.bean.StarInfoBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 星座Fragment
 *      ViewPager和GridView
 */
public class StarFragment extends Fragment {
    ViewPager starvp;
    GridView  stargv;
    LinearLayout pointLayout;
    private List<StarInfoBean.StarinfoBean> mDatas;
    private StarBaseAdapter starBaseAdapter;

    //图片数组
    int []imgIds={R.mipmap.pic_guanggao,R.mipmap.pic_star,R.mipmap.pic_star1,R.mipmap.pic_star2,R.mipmap.pic_star3,R.mipmap.pic_star4,R.mipmap.pic_star5,R.mipmap.pic_star6,R.mipmap.pic_star7,R.mipmap.pic_star8,R.mipmap.pic_star9,R.mipmap.pic_star10};
    //ViewPager的数据源
    List<ImageView> ivList;
    //管理指示器小圆点的集合
    List<ImageView> pointList;
    //定时轮播图实现自动轮播
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1){
                //获取当前显示的页面
                int currentItem=starvp.getCurrentItem();
                //判断是否为最后一张,如果是回到第一张,否则显示下一张
                if(currentItem==ivList.size()-1){
                    starvp.setCurrentItem(0);
                }else {
                    currentItem++;
                    starvp.setCurrentItem(currentItem);
                }
                //形成循环发送---接受消息的效果,在接收消息的同时也要发送消息
                handler.sendEmptyMessageDelayed(1,4000);
            }
        }
    };
    StarPagerAdapter starPagerAdapter;
    public StarFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_star, container, false);
        initView(view);
        Bundle bundle=getArguments();
        StarInfoBean infoBean=(StarInfoBean)bundle.getSerializable("info");
        mDatas=infoBean.getStarinfo();//获取关于星座信息的集合数据
        //创建适配器
        starBaseAdapter=new StarBaseAdapter(getContext(),mDatas);
        stargv.setAdapter(starBaseAdapter);
        initPager();//显示轮播图
        setVPListener();//设置轮播图监听器
        setGVListener();//设置gridview监听事件
        //延迟4秒发送一条消息,通知可以切换轮播图图片了
        handler.sendEmptyMessageDelayed(1,4000);
        return view;
    }
    /*设置ViewPager显示的页面*/
    private void initPager(){
        ivList=new ArrayList<>();
        pointList=new ArrayList<>();
        for(int i=0;i<imgIds.length;i++){
            ImageView iv=new ImageView(getContext());
            iv.setImageResource(imgIds[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            //设置图片宽高
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(lp);
            //将图片view加载到集合当中
            ivList.add(iv);
            //创建图片对应的指示器小圆点
            ImageView piv=new ImageView(getContext());
            piv.setImageResource(R.mipmap.point_normal);
            LinearLayout.LayoutParams lpl=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            lpl.setMargins(20,0,0,0);
            piv.setLayoutParams(lpl);
            //将小圆点添加到布局中
            pointLayout.addView(piv);
            //将校园贷你添加到统一管理的集合中
            pointList.add(piv);
        }
        //默认第一个为被选中状态
        pointList.get(0).setImageResource(R.mipmap.point_focus);
        starPagerAdapter=new StarPagerAdapter(getContext(),ivList);
        starvp.setAdapter(starPagerAdapter);
    }
    //设置viewpager的监听器函数
    private void setVPListener(){
        starvp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (int i=0;i<pointList.size();i++){
                    pointList.get(i).setImageResource(R.mipmap.point_normal);
                }
                pointList.get(position).setImageResource(R.mipmap.point_focus);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 设置gridview的监听事件函数
     */
    private void setGVListener(){
        stargv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StarInfoBean.StarinfoBean bean=mDatas.get(i);
                Intent intent=new Intent(getContext(),StarAnalysisActivity.class);
                intent.putExtra("star",bean);
                startActivity(intent);
            }
        });
    }
    private void initView(View view){
        starvp=view.findViewById(R.id.starfrag_vp);
        stargv=view.findViewById(R.id.starfrag_gv);
        pointLayout=view.findViewById(R.id.starfrag_layout);
    }
}
